//
//  YGVisitorView.swift
//  sinaWeiBoGui
//
//  Created by liyonggui on 16/5/10.
//  Copyright © 2016年 liyonggui. All rights reserved.
//

import UIKit

// 定义代理协议
@objc protocol YGVisitorViewDelegate: NSObjectProtocol {
    
    // 定义协议方法
    // 注册
    func visitorViewDidClickRegister()
    
    // 登录
    func visitorViewDidClickLogin()
    
}

class YGVisitorView: UIView {

    // 代理属性
    weak var delegate:YGVisitorViewDelegate?
    
    // MARK: - 公开的方法
    func setupVisitorInfo(imageName: String, message: String) {
        
        // 将转轮图片替换
        wheelView.image = UIImage(named: imageName)
        
        descView.text = message
        
        // 隐藏小房子
        homeView.hidden = true
        
        // 隐藏遮盖
        coverView.hidden = true
    }
    
    // 转轮旋转动画
    func startRotationAnimation() {
        
        // 创建动画
        let anima = CABasicAnimation(keyPath: "transform.rotation")
        
        anima.toValue = M_PI * 2
        anima.duration = 20
        anima.repeatCount = MAXFLOAT
        
        // 核心动画完成的时候不移除
        anima.removedOnCompletion = false
        
        // 添加到图层上
        wheelView.layer.addAnimation(anima, forKey: nil)
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        // 设置UI
        prepareUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func prepareUI() {
//        self.backgroundColor = UIColor.blueColor()
        // 设置背景颜色
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        
        // 将子控件添加到父容器
        addSubview(wheelView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(descView)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 添加约束
        addRestrain()
        
    }
    
    /// 添加约束
    private func addRestrain() {
        
        wheelView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        descView.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        
        // 转轮
        addConstraint(NSLayoutConstraint(item: wheelView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: wheelView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        // 遮盖
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: .Equal, toItem: loginButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        // 小房子
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: wheelView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: wheelView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // 描述
        addConstraint(NSLayoutConstraint(item: descView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: wheelView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: descView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        addConstraint(NSLayoutConstraint(item: descView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // 注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: wheelView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: descView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        // 登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: wheelView, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: descView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
    }
    
    // MARK: - 点击事件
    @objc private func registerButtonClick() {
//        print("registerButtonClick")
        delegate?.visitorViewDidClickLogin()
    }
    
    @objc private func loginButtonClick() {
//        print("loginButtonClick")
        delegate?.visitorViewDidClickRegister()
    }
    
    // MARK: - 懒加载
    /// 转轮
    private lazy var wheelView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    /// 遮盖
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    /// 小房子
     private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    /// 描述
    private lazy var descView: UILabel = {
       
        let label = UILabel()
        
        label.text = "关注一些人，看看有什么惊喜"
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(16)
        label.textAlignment = NSTextAlignment.Center
        label.sizeToFit()
        label.numberOfLines = 0
        
        return label
    }()
    
    /// 注册按钮
    private lazy var registerButton: UIButton = {
        
        let button = UIButton()
        
        // 设置相关属性
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        // 添加点击事件
        button.addTarget(self, action: "registerButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    /// 登录按钮
    private lazy var loginButton: UIButton = {
        
        let button = UIButton()
        
        // 设置相关属性
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        // 添加点击事件
        button.addTarget(self, action: "loginButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return button
    }()

}
